package com.gwkj.zxjl_user.Utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.gwkj.zxjl_user.Bean.Base.ListDataBean;

import java.lang.reflect.Type;
import java.util.ArrayList;

/**
 * Gson 的简单封装
 * Created by fujh on 16/4/6.
 */
public class GsonUtils {

    private static Gson gson = null;

    static {
        if (gson == null) {
            gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        }
    }

    private GsonUtils() {
    }

    /**
     * 转成json
     *
     * @param object
     * @return
     */
    public static String GsonString(Object object) {
        String gsonString = null;
        if (gson != null) {
            gsonString = gson.toJson(object);
        }
        return gsonString;
    }

    /**
     * 转成bean
     *
     * @param gsonString
     * @param cls
     * @return
     */
    public static <T> T GsonToBean(String gsonString, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(gsonString, cls);
        }
        return t;
    }

    /**
     * 转成list
     *
     * @param gsonString
     * @param type
     * @return
     */
    public static <T> ArrayList<T> GsonToList(String gsonString, Type type) {
        ArrayList<T> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString, type);
        }
        return list;
    }

    /**
     * 转成bean
     *
     * @param object
     * @param cls
     * @return
     */
    public static <T> T GsonToBean(Object object, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(gson.toJson(object), cls);
        }
        return t;
    }

    /**
     * 转成list
     *
     * @param objects
     * @param type
     * @return
     */
    public static <T> ArrayList<T> GsonToList(ArrayList<Object> objects, Type type) {
        ArrayList<T> list = null;
        if (gson != null) {
            list = gson.fromJson(gson.toJson(objects), type);
        }
        return list;
    }

    public static <T> ArrayList<T> GsonToList(Object objects, Type type) {
        ListDataBean listDataBean = GsonToBean(objects, ListDataBean.class);
        ArrayList<T> list = null;
        list = GsonToList(listDataBean.getData(), type);
        return list;
    }

    /**
     * 转成list中有map的
     *
     * @param gsonString
     * @return
     */
    /*public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
        List<Map<String, T>> list = null;
        if (gson != null) {
            list = gson.fromJson(gsonString,
                    new TypeToken<List<Map<String, T>>>() {
                    }.getType());
        }
        return list;
    }*/

    /**
     * 转成map的
     *
     * @param gsonString
     * @return
     */
    /*public static <T> Map<String, T> GsonToMaps(String gsonString) {
        Map<String, T> map = null;
        if (gson != null) {
            map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.getType());
        }
        return map;
    }*/
}
